<?php
class EventTypesController extends AppController {

	var $name = 'EventTypes';
	var $helpers = array('Html', 'Form');
	
    function beforeFilter(){ 
        $this->__validateLoginStatus();
       $accessArray = $this->Session->read('accessArray');
        if (!$accessArray['CreateEvent']) {
        	$this->Session->setFlash('You don\'t have the privilage to access this page');
        	$this->redirect(array('controller' => HOME_CONTROLLER, 'action' => 'index'));
        }
     }

    function __validateLoginStatus(){ 
       if($this->Session->check('User') == false){
          $this->Session->setFlash('you must be logged in to access this page');
          $this->redirect(array('controller'=>'users','action'=>'login'));
       }
    }	

	function index() {
		$this->EventType->recursive = 0;
		$this->set('eventTypes', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid EventType', true));
			$this->redirect(array('action' => 'index'));
		}
		$type = $this->EventType->find('first', array(
										'conditions' => array(
											'EventType.id' => $id
										),
										'recursive' => -1
									)
								);
		if(!$type){
			$this->Session->setFlash(__('Invalid event type',true));
			$this->redirect(array('controller'=>HOME_CONTROLLER,'action'=>'index'));
		}
		$this->set('eventType', $type);
	}

	function add() {
		if (!empty($this->data)) {
			$this->EventType->create();
			if ($this->EventType->save($this->data)) {
				$this->Session->setFlash(__('The EventType has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The EventType could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid EventType', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->EventType->save($this->data)) {
				$this->Session->setFlash(__('The EventType has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The EventType could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$type = $this->EventType->read(null,$id);
			if(!$type){
				$this->Session->setFlash(__('Invalid event type',true));
				$this->redirect(array('controller'=>HOME_CONTROLLER,'action'=>'index'));	
			}
			$this->data = $this->EventType->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for EventType', true));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->EventType->del($id)) {
			$this->Session->setFlash(__('EventType deleted', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The EventType could not be deleted. Please, try again.', true));
		$this->redirect(array('controller'=>HOME_CONTROLLER,'action' => 'index'));
	}

}
?>